package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Client;
import org.ahuanlien.crm.query.ClientQueryObject;
import org.ahuanlien.crm.service.IClientService;
import org.ahuanlien.crm.util.JSONResult;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {

	@Autowired
	private IClientService clientService;

	//潜在客户 potential client
	@RequestMapping("pc")
	public String pc() {
		return "client/pc";
	}

	//正式客户 formal client
	@RequestMapping("fc")
	public String fc() {
		return "client/fc";
	}

	@RequestMapping("pool")
	public String pool() {
		return "client/pool";
	}

	@RequestMapping("query")
	@ResponseBody
	public PageResult query(ClientQueryObject qo) {
		PageResult result = clientService.query(qo);
		return result;
	}

	@RequestMapping("getClient")
	@ResponseBody
	public Client selectByPrimaryKey(Long id) {
		Client client = clientService.selectByPrimaryKey(id);
		return client;
	}

	@RequestMapping("querySrouce")
	@ResponseBody
	public PageResult querySrouce(ClientQueryObject qo) {
		PageResult result = clientService.querySrouce(qo);
		return result;
	}

	//保存或编辑
	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Client client) {
		try {
			clientService.saveOrUpdate(client);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	//吸纳客户
	@RequestMapping("absorbClient")
	@ResponseBody
	public JSONResult absorbClient(Long clientId) {
		try {
			clientService.absorbClient(clientId);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	//客户移入资源池
	@RequestMapping("moveResource")
	@ResponseBody
	public JSONResult moveResource(Long clientId) {
		try {
			clientService.moveResource(clientId);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

}
